package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.constant.RabbitConstant;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/test")
    public Result test(){
        CopyOnWriteArrayList list = new CopyOnWriteArrayList();
        list.add("11");
        rabbitTemplate.convertAndSend(RabbitConstant.ACCOUNT_INIT_EXCHANGE+"1",
                RabbitConstant.ACCOUNT_INIT_ROUTING,"111",new CorrelationData());
        return Result.ok();
    }


    /**
     * 微信登录授权
     * @param code
     * @return
     */
    @GetMapping("/wxLogin/{code}")
    public Result wxLogin(@PathVariable("code") String code){
        return Result.ok(userInfoService.wxLogin(code));
    }

    /**
     * 获取用户登录信息
     * @return
     */
    @GuiGuLogin
    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        UserInfoVo userInfoVo = userInfoService.getUserInfoByUserId(userId);
        return Result.ok(userInfoVo);
    }

    /**
     * 更新用户信息
     * @param userInfoVo
     * @return
     */
    @GuiGuLogin
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserInfoVo userInfoVo){
        userInfoService.updateUser(userInfoVo);
        return Result.ok();
    }

}
